package com.inifitness.tenant.core.system.controller.common;

import com.inifitness.common.result.Result;
import com.inifitness.tenant.core.system.model.dto.CaptchaResult;
import com.inifitness.tenant.core.system.model.dto.LoginResult;
import com.inifitness.tenant.core.system.service.BizAuthService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "01.认证中心")
@RestController
@RequestMapping("/api/v1/auth")
@RequiredArgsConstructor
@Log4j2
public class AuthController {

  private final BizAuthService bizAuthService;

  @Operation(summary = "登录")
  @PostMapping("/login")
  public Result<LoginResult> login(
      @Parameter(description = "用户名", example = "admin") @RequestParam String username,
      @Parameter(description = "密码", example = "123456") @RequestParam String password
  ) {
    LoginResult loginResult = bizAuthService.login(username, password);
    return Result.success(loginResult);
  }

  @Operation(summary = "注销")
  @DeleteMapping("/logout")
  public Result<Void> logout() {
    bizAuthService.logout();
    return Result.success();
  }

  @Operation(summary = "获取验证码")
  @GetMapping("/captcha")
  public Result<CaptchaResult> getCaptcha() {
    CaptchaResult captcha = bizAuthService.getCaptcha();
    return Result.success(captcha);
  }

}
